package cn.edu.bupt.ch6_2.controller;

import cn.edu.bupt.ch6_2.common.Result;
import cn.edu.bupt.ch6_2.entity.Product;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Tag(name = "系统登陆")
@RestController
@RequestMapping("/api/v1/sys")
public class SysController {
    @Operation(summary = "登陆", description = "")
    @GetMapping(path = "/login", produces = "application/json")
    public Result<?> login(HttpServletRequest request, String username, String password) {
        HttpSession session = request.getSession();
        session.setAttribute("username",username);
        return Result.ok();
    }


    @Operation(summary = "返回当前用户的用户名", description = "")
    @GetMapping(path = "/my", produces = "application/json")
    public Result<?> my(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("username") != null){
            String username = session.getAttribute("username").toString();
            return Result.ok(username);
        }
        return Result.error("未登陆");
    }


    @Operation(summary = "注销", description = "")
    @GetMapping(path = "/logout", produces = "application/json")
    public Result<?> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("username");
        return Result.ok();
    }

}
